package com.wanlian.router.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Hyu on 2018/10/17.
 * Email: fvaryu@163.com
 */
public class PieChart extends View {

    private RectF mRectF = new RectF();
    private Paint mFreePint;

    public PieChart(Context context) {
        super(context);
        init();
    }

    public PieChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mFreePint = new Paint();
        mFreePint.setAntiAlias(true);
        mFreePint.setStrokeCap(Paint.Cap.SQUARE);
        mFreePint.setStyle(Paint.Style.STROKE);
        mFreePint.setColor(Color.parseColor("#ff00ff"));
        mFreePint.setStrokeWidth(50.f);
//        SweepGradient sweepGradient = new SweepGradient()

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        mRectF.set(getLeft(), getTop(), w, h);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        int width = getDefaultSize(getSuggestedMinimumHeight(), widthMeasureSpec);
        int min = Math.min(height, width);
        setMeasuredDimension(min, min);
        mRectF.set(getLeft() + 30, getTop() + 30, min - 30, min - 30);


    }

    Path mPath = new Path();
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mFreePint.setShader( new SweepGradient(mRectF.centerX(),
                mRectF.centerY(), Color.parseColor("#1A59E2"), Color.parseColor("#2292F5")));

        mPath.addArc(mRectF, 270, 90);
//        mFreePint.setShader(new LinearGradient(0,0,100,100,colors,positions, Shader.TileMode.MIRROR));
//        canvas.drawArc(mRectF, 270, 90, false, mFreePint);

        canvas.drawPath(mPath, mFreePint);
        mPath.close();
//        mFreePint.setColor(Color.WHITE);
//        mFreePint.setShader(null);
//
//        canvas.drawArc(mRectF, 0,2 , false, mFreePint);
//
//        mFreePint.setShader( new SweepGradient(mRectF.centerX(),
//                mRectF.centerY(), Color.parseColor("#29A8FA"), Color.parseColor("#21D7F2")));
//        canvas.drawArc(mRectF, 2,268 , false, mFreePint);
//
//        mFreePint.setColor(Color.WHITE);
//        mFreePint.setShader(null);
//
//        canvas.drawArc(mRectF, 268,2 , false, mFreePint);

//        mFreePint.setStrokeWidth(30.f);
//        canvas.drawArc(mRectF, 20,200 , false, mFreePint);
    }


}
